#include "AnimationReader.h"
#include "StringUtil.h"
#include "io.h"


AnimationReader::AnimationReader(tstring filename):TextReader(filename)
{
}

AnimationReader::~AnimationReader(void)
{
}

bool AnimationReader::Read()
{
	tstring result;
	tstring line;
	tifstream myfile(GetFileName().c_str());
	if (myfile.is_open())
	{
		while (! myfile.eof() )
		{
			getline (myfile,line);
			result += line;
			result += _T("\n");
		}
		xml_document<tchar> doc;
		tchar* toParse = (tchar*)result.c_str();
		doc.parse<0>(toParse);
		xml_node<tchar> *root = doc.first_node(_T("animation"));
		tstring sname,sstartframe,sstopframe;
		for (xml_attribute<tchar> *attr = root->first_attribute();
			attr; attr = attr->next_attribute())
		{
			tstring name(attr->name());
			if ( name == _T("name") ){
				sname.assign(attr->value());
			}else if ( name == _T("startFrame")){
				sstartframe.assign(attr->value());
			}else if ( name == _T("endFrame")){
				sstopframe.assign(attr->value());
			}
		}
		m_pAnimation = new Animation(sname,ParseInt(sstartframe),ParseInt(sstopframe));
		
		for ( xml_node<tchar> *anim = root->first_node(_T("anim"));
			anim; anim = anim->next_sibling())
		{
			ParseAnim(anim);
		}
		myfile.close();
		return true;
	}
	return false;
}

void AnimationReader::ParseAnim(xml_node<tchar> *anim)
{
	xml_attribute<tchar> *attr = anim->first_attribute();
	tstring attName(attr->name());
	if ( attName == _T("name")){
		tstring n (attr->value());
		m_pStoredAnimationNode = new StoredAnimationNode(n);
		m_pAnimation->AddAnimationNode(m_pStoredAnimationNode);
	}

	for ( xml_node<tchar> *time = anim->first_node(_T("time"));
			time; time = time->next_sibling())
	{
		ParseTimeTransform(time);
	}
}

void AnimationReader::ParseTimeTransform(xml_node<tchar> *timetransform)
{
	xml_attribute<tchar> *attr = timetransform->first_attribute();
	tstring attName(attr->name());
	if ( attName == _T("value")){
		tstring n (attr->value());
		m_CurrentTime = this->ParseInt(n);
	}

	for ( xml_node<tchar> *transform = timetransform->first_node(_T("transform"));
			transform; transform = transform->next_sibling())
	{
		tstring quat,pivot,scale;

		for (xml_attribute<tchar> *attr = transform->first_attribute();
			attr; attr = attr->next_attribute())
		{
			tstring name(attr->name());
			if ( name == _T("rot") ){
				quat.assign(attr->value());
			}else if ( name == _T("pivot")){
				pivot.assign(attr->value());
			}else if ( name == _T("scale")){
				scale.assign(attr->value());
			}
		}
		float x, y,z,w;
		w=0.0f;
		//swscanf_s(quat.c_str(),L"%f %f %f %f", &x , &y, &z ,&w);
		swscanf_s(quat.c_str(),L"%f %f %f", &x , &y, &z);
		float px,py,pz;
		swscanf_s(pivot.c_str(),L"%f %f %f",&px,&py,&pz);
		
		float sx,sy,sz;
		swscanf_s(scale.c_str(),L"%f %f %f",&sx,&sy,&sz);
		if (scale.size() == 0 )
			m_pStoredAnimationNode->AddTransformAt(m_CurrentTime,DAEFloat4(x,y,z,w),DAEFloat3(px,py,pz));
		else
			m_pStoredAnimationNode->AddTransformAt(m_CurrentTime,DAEFloat4(x,y,z,w),DAEFloat3(px,py,pz),DAEFloat3(sx,sy,sz));

	}
}